package com.cloud.cloudshop.shop.controller;

import com.cloud.cloudshop.client.UidServiceClient;
import com.cloud.cloudshop.provider.RabbitCreateOrderAndSend;
import com.cloud.cloudshop.shop.redisdao.RedisUserGoodsRepository;
import com.cloud.cloudshop.shop.service.GoodsCatesService;
import com.cloud.cloudshop.shop.service.GoodsService;
import com.cloud.cloudshop.util.PageUtils;
import com.cloud.common.entity.GoodsEntity;
import com.cloud.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * @author luoJie
 * @email luoJie@gmail.com
 * @date 2020-10-04 16:40:24
 */
@Data
@Slf4j
@RestController
@Api(value = "我的api")
@RequestMapping("shop/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsCatesService goodsCatesService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisUserGoodsRepository redisUserGoodsRepository;

    @Autowired
    private RabbitCreateOrderAndSend rabbitCreateOrderAndSend;

    @Autowired
    private UidServiceClient uidServiceClient;



    /**
     * 列表
     */
    @RequestMapping("/list/{cateId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("cateId") Long cateId) {
        PageUtils page = goodsService.queryPage(params, cateId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        GoodsEntity goods = goodsService.getById(id);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GoodsEntity goods) {

        goodsService.addGoods(goods);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GoodsEntity goods) {
        goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除商品")
    @RequestMapping(value = "/delete")
    public R delete(@RequestBody String[] ids) {
        goodsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
